package com.css.bjdt_report.report.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.css.bjdt_report.report.entity.RptGroup;
import com.css.bjdt_report.report.entity.RptTemplate;
import com.css.bjdt_report.report.service.ReportGroupManageService;
import com.css.bjdt_report.report.service.ReportTemplateManageService;

@Controller
@RequestMapping(value = "/reportTemplateManageController")
public class ReportTemplateManageController {

	@Autowired
	private ReportTemplateManageService reportTemplateManageService;

	@Autowired
	private ReportGroupManageService reportGroupManageService;

	//跳转到子报表信息录入页面
	@RequestMapping(value = "/preCreate/{groupId}/{annualId}", method = RequestMethod.GET)
	public String preCreate(@PathVariable String groupId, @PathVariable String annualId, Model model) {
		RptGroup rptGroup = reportGroupManageService.queryByID(groupId);
		RptTemplate rptTemplate = new RptTemplate();
		rptTemplate.setGroupName(rptGroup.getName());
		rptTemplate.setGroupId(groupId);
		model.addAttribute("annualId", annualId);
		model.addAttribute("rptTemplate", rptTemplate);
		return "/report-manage/report-manage-template-input";
	}

	//保存子报表信息
	@RequestMapping(value = "/saveTemplate", method = RequestMethod.POST)
	public String saveTemplate(RptTemplate rptTemplate, @RequestParam(value = "annualId") String annualId,
			Model model) {
		reportTemplateManageService.saveTemplate(rptTemplate);
		model.addAttribute("annualId", annualId);
		model.addAttribute("groupId", rptTemplate.getGroupId());
		model.addAttribute("message", "操作成功");
		return "/report-manage/report-manage-template-input";
	}

	//子报表信息明细
	@RequestMapping(value = "/templateDetail/{templateId}", method = RequestMethod.GET)
	public String templateDetail(@PathVariable String templateId, Model model) {
		model.addAttribute("rptTemplate", reportTemplateManageService.getAnRptTemplate(templateId));
		return "/report-manage/report-manage-template-detail";
	}

	//删除一个子表
	@ResponseBody
	@RequestMapping(value = "/reportTemplateDelete/{templateId}/{annualId}/{groupId}", method = RequestMethod.POST)
	public List<String> deleteAnnual(@PathVariable String templateId, @PathVariable String annualId,
			@PathVariable String groupId) {
		reportTemplateManageService.deleteAnRptTemplate(templateId, annualId);
		List<String> strList = new ArrayList<String>();
		strList.add(annualId);
		strList.add(groupId);
		return strList;
	}

	//子报表修改预处理
	@RequestMapping(value = "/preUpdateTemplate/{templateId}/{annualId}/{groupId}", method = RequestMethod.GET)
	public String preUpdateTemplate(@PathVariable String templateId, @PathVariable String annualId,
			@PathVariable String groupId, Model model) {
		RptGroup rptGroup = reportGroupManageService.queryByID(groupId);
		RptTemplate rptTemplate = new RptTemplate();
		rptTemplate.setGroupName(rptGroup.getName());
		rptTemplate.setGroupId(groupId);
		model.addAttribute("annualId", annualId);
		model.addAttribute("rptTemplate", reportTemplateManageService.getAnRptTemplate(templateId));
		return "/report-manage/report-manage-template-input";
	}

	//保存子报表信息
	@RequestMapping(value = "/updateTemplate", method = RequestMethod.POST)
	public String updateTemplate(RptTemplate rptTemplate, @RequestParam(value = "annualId") String annualId,
			Model model) {
		reportTemplateManageService.updateAnRptTemplate(rptTemplate);
		model.addAttribute("annualId", annualId);
		model.addAttribute("groupId", rptTemplate.getGroupId());
		model.addAttribute("message", "操作成功");
		return "/report-manage/report-manage-template-input";
	}
}
